const http = require('http')
const data = require('./index.json')
const app = http.createServer((req, res) => {
    let str = `<ul>`
    res.setHeader('content-type', 'text/html;charset=utf-8')
    let u = new URL(req.url, 'https://127.0.0.1:80')
    let { pathname } = u
    if (pathname === '/list') {
        data.forEach((item) => {
            return str += `<li><a href='/detail?id=${item.id}'>${item.newsTitle}</a></li>`
        })
        str += '</ul>'
        res.write(`<h1>新闻列表页</h1>`)
        res.end(str)
    }
    else if (pathname === '/detail') {
        let id = u.searchParams.get('id')
        let st = data.find((item)=>{
            return item.id === id
        })
        res.end(`<h1>商品详情页</h1>`+st.newsContent)
    }
    else {
        res.end('路径错误')
    }
})
app.listen(80, () => {
    console.log('端口80已经启动');
})